<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <div>
        <el-form-item label="账单日">
          <el-date-picker
            clearable
            v-model="queryParams.billDay"
            type="datetime"
            value-format="yyyy-MM-dd"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>至
        <el-form-item label="">
          <el-date-picker
            clearable
            v-model="queryParams.billDay1"
            type="datetime"
            value-format="yyyy-MM-dd"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
      </div>


      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="batchList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="对账批次" align="center" prop="batchNo" />
      <el-table-column label="账单日" align="center" prop="billDay" />
      <el-table-column label="账单类型" align="center" prop="billType">
        <template slot-scope="scope">
          <span v-for="item in billTypeList" v-if="scope.row.billType==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="渠道名称" align="center">拉卡拉</el-table-column>
      <el-table-column label="渠道对账文件交易数量" align="center" prop="checkCount" />
      <el-table-column label="系统交易流水数量" align="center" prop="orderCount" />
      <el-table-column label="差错账数量" align="center" prop="diffCount" />
      <el-table-column label="对账日期" align="center" prop="checkDay" />
      <el-table-column label="生成方式" align="center" prop="createType">
        <template slot-scope="scope">
          <span v-for="item in createTypeList" v-if="scope.row.createType==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否完成对账" align="center" prop="completeState">
        <template slot-scope="scope">
          <span v-for="item in completeStateList" v-if="scope.row.completeState==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="对账确认时间" align="center" prop="confirmDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.confirmDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="showBatch(scope.row)"
            v-hasPermi="['finance:batch:edit']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="80%">
      <span>
          <el-form :inline="true" :model="checkBatchForm" class="demo-form-inline">
            <div>
                <el-form-item label="渠道名称">
              <el-input v-model="checkBatchForm.tradeQudao" placeholder="" clearable></el-input>
            </el-form-item>
            <el-form-item label="交易结果">
              <el-select v-model="checkBatchForm.tradeResult" placeholder="请选择" clearable>
                <el-option v-for="item in tradeResultList" :label="item.dictLabel" :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
            </div>
            <div>
                <el-form-item label="对账批次">
                  <el-input v-model="checkBatchForm.batchNo" placeholder="" clearable></el-input>
                </el-form-item>
                <el-form-item label="交易金额">
                  <el-input v-model="checkBatchForm.amount" placeholder="" clearable></el-input>
                </el-form-item>
            </div>
            <div>
                <el-form-item label="是否差错">
                  <el-select v-model="checkBatchForm.hasDiff" placeholder="请选择" clearable>
                    <el-option v-for="item in hasDiffList" :label="item.dictLabel" :value="item.dictValue"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="交易日期">
                  <el-date-picker
                    v-model="checkBatchForm.startDate"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    clearable
                    type="datetime"
                    placeholder="选择日期时间">
                  </el-date-picker>
                  <span>至</span>
                  <el-date-picker
                    v-model="checkBatchForm.endDate"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    clearable
                    type="datetime"
                    placeholder="选择日期时间">
                  </el-date-picker>
                </el-form-item>
            </div>
             <div>
                <el-form-item label="处理结果">
                   <el-select v-model="checkBatchForm.processState" placeholder="请选择" clearable>
                   <el-option v-for="item in processStateList" :label="item.dictLabel" :value="item.dictValue"></el-option>
                   </el-select>
                </el-form-item>
            <el-form-item label="支付方式">
              <el-select v-model="checkBatchForm.payWay" placeholder="请选择" clearable>
               <el-option v-for="item in payWayList" :label="item.dictLabel" :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
            </div>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
          </el-form>
          <el-table v-loading="loading" :data="checkBatchDetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="对账批次" align="center" prop="batchNo" />
      <el-table-column label="交易流水号" align="center" prop="payOrderNo" />
      <el-table-column label="渠道订单号" align="center" prop="qudaoOrderNo" />
      <el-table-column label="渠道名称" align="center" prop="tradeQudao"></el-table-column>
      <el-table-column label="交易结果" align="center" prop="tradeResult">
          <template slot-scope="scope">
          <span v-for="item in tradeResultList" v-if="scope.row.tradeResult==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="交易金额" align="center" prop="amount"></el-table-column>
      <el-table-column label="交易日期" align="center" prop="tradeDateZf"></el-table-column>
      <el-table-column label="手续费" align="center" prop="commissionFee"></el-table-column>
      <el-table-column label="支付方式" align="center" prop="payWay">
         <template slot-scope="scope">
          <span v-for="item in payWayList" v-if="scope.row.payWay==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="交易账号" align="center" prop="tradeAccount"></el-table-column>
      <el-table-column label="交易账单类型" align="center" prop="billType">
        <template slot-scope="scope">
          <span v-for="item in billTypeList" v-if="scope.row.billType==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="对账完成时间" align="center" prop="checkEndDate"></el-table-column>
      <el-table-column label="是否差错" align="center" prop="hasDiff">
         <template slot-scope="scope">
          <span v-for="item in hasDiffList" v-if="scope.row.hasDiff==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理结果" align="center" prop="processState">
         <template slot-scope="scope">
          <span v-for="item in processStateList" v-if="scope.row.processState==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
         <span v-if="scope.row.processState==1 && scope.row.hasDiff==true">
             <el-button
               type="info"
               @click="errorHanding(scope.row)"
             >差错处理</el-button>
          </span>
        </template>
      <!--  <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="errorHanding(scope.row)"
          >差错处理</el-button>
        </template>-->
      </el-table-column>
    </el-table>
        <el-button type="warning" @click="forShure">重新对账</el-button>
        <el-button type="text" @click="moneyForsure">对账确认</el-button>
      </span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { forShureInfo,listBatch, addBatch, updateBatch,getCompleteStateList,getBillTypeList,getCreateTypeList,batchInfo,dictInfo,moneyInfo,processCheckDifResult } from "@/api/finance/batch";

export default {
  name: "Batch",
  components: {
  },
  data() {
    return {
      //交易结果
      tradeResultList:[],
      //是否差错
      hasDiffList:[],
      //处理结果
      processStateList:[],
      //支付方式
      payWayList:[],
      checkBatchDetailList:[],
      checkBatchForm:{},
      dialogVisible: false,
      //是否完成对账
      completeStateList:[],
      //生成方式
      createTypeList:[],
      //账单类型
      billTypeList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 对账批次表格数据
      batchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billDay: null,
        checkDay: null,
        batchNum: null,
        batchNo: null,
        billType: null,
        orderCount: null,
        checkCount: null,
        diffCount: null,
        completeState: null,
        confirmDate: null,
        createType: null,
        downloadBatch: null,
        remarks: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        billDay: [
          { required: true, message: "账期年月日不能为空", trigger: "blur" }
        ],
        checkDay: [
          { required: true, message: "对账年月日(yyyymmdd)不能为空", trigger: "blur" }
        ],
        batchNum: [
          { required: true, message: "同一天内的批次序号(对账年月日内)不能为空", trigger: "blur" }
        ],
        batchNo: [
          { required: true, message: "批次号不能为空", trigger: "blur" }
        ],
        createUserid: [
          { required: true, message: "创建用户ID不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateUserid: [
          { required: true, message: "更新用户ID不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getCompleteStateList();
    this.getBillTypeList();
    this.getCreateTypeList();
    this.getDict();
  },
  methods: {

    ////点击差错处理按钮进行处理差错值
    errorHanding(row) {
      if (confirm("是否进行差错处理")) {
        let payWayNum = null;
        console.log(row)
        if (row.processState != 0) {
          // 有商户可结算金额，并且金额大于0的时候
          if (row.canClearingAmount != '' && row.canClearingAmount > 0) {
            if (confirm("是否需要结算？")) {
              payWayNum = 2;
              //差错处理方法processCheckDifResult
              processCheckDifResult({batchId: row.id, processState: payWayNum}).then(res => {
                console.log("2=================>>>>", res)
                if (res.code == 200) {
                  // this.$message.success("差错处理成功");
                  window.confirm("差错处理成功")
                  location.reload();
                } else {
                  window.confirm(res.msg)
                  // this.$message.error(res.msg)
                }
              })
            } else {
              payWayNum = 3;
              //差错处理方法
              processCheckDifResult({batchId: row.id, processState: payWayNum}).then(res => {
                console.log("3=================>>>>", res)
                if (res.code == 200) {
                  // this.$message.success("差错处理成功");
                  window.confirm("差错处理成功")
                  location.reload();
                } else {
                  window.confirm(res.msg)
                  // this.$message.error(res.msg)
                }
              })
            }
          } else {// 无结算金额的时候，线下处理完差错，线上只修改一下处理状态，无需结算
            payWayNum = 4;
            //处理差错
            processCheckDifResult({batchId: row.id, processState: payWayNum}).then(res => {
              console.log("4=================>>>>", res)
              if (res.code == 200) {
                // this.$message.success("差错处理成功");
                window.confirm("差错处理成功")
                location.reload();
              } else {
                window.confirm(res.msg)
                // this.$message.error(res.msg)
              }
            })

          }
        }
      } else {
        return;
      }
    },
    //对账确认
    moneyForsure() {
        moneyInfo({batchNo:this.checkBatchForm.batchNo}).then(res=>{
            console.log("对账确认==============",res)
        })
        this.getShowBatch();
    },
    //重新对账
    forShure() {

      var batchType = 3;
      forShureInfo({batchNo:this.checkBatchForm.batchNo,batchType:batchType}).then(res=>{
          console.log("重新对账===",res);
      })

      this.getShowBatch();
    },
    getDict() {
      dictInfo().then(res=>{
        this.tradeResultList=res.data.tradeResultList.data
        this.hasDiffList=res.data.hasDiffList.data
        this.processStateList=res.data.processStateList.data
        this.payWayList=res.data.payWayList.data
      })
    },
    onSubmit() {
      this.getShowBatch();
    },
    getShowBatch() {
      batchInfo(this.checkBatchForm).then(res=>{
        console.log("===========",res)
        this.checkBatchDetailList=res.rows
      })
    },
    showBatch(row){
      console.log("查看---------",row);
      this.dialogVisible=true;
      this.checkBatchForm.batchNo=row.batchNo;
      this.getShowBatch();

    },
    //获取账单类型集合
    getBillTypeList() {
      this.loading = true;
      getBillTypeList().then(res => {
        this.billTypeList = res.data;
      });
    },
    //获取生成方式集合
    getCreateTypeList() {
      getCreateTypeList().then(res => {
        this.createTypeList = res.data;
      });
    },
    //是否完成对账
    getCompleteStateList() {
      getCompleteStateList().then(res => {
        console.log("------------------->",res)
        this.completeStateList = res.data;
      });
    },
    /** 查询对账批次列表 */
    getList() {
      listBatch(this.queryParams).then(response => {
        this.batchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        billDay: null,
        checkDay: null,
        batchNum: null,
        batchNo: null,
        billType: null,
        orderCount: null,
        checkCount: null,
        diffCount: null,
        completeState: null,
        confirmDate: null,
        createType: null,
        downloadBatch: null,
        remarks: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBatch(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBatch(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
